package org.jeecg.modules.iot.mqtt.server;

import java.nio.ByteBuffer;

public class JT808VehicleControlMessage {
    public static void main(String[] args) {



        byte controlCommand = 0x01; // 车辆控制指令，例如熄火指令
        byte controlParameter = 0x00; // 车辆控制参数，例如熄火参数

        byte[] vehicleControlData = generateVehicleControlData(controlCommand, controlParameter);

        // 将字节数组转换为十六进制字符串
        StringBuilder hexString = new StringBuilder();
        for (byte b : vehicleControlData) {
            hexString.append(String.format("%02X ", b));
        }

        System.out.println("生成的车辆控制数据：");
        System.out.println(hexString.toString());
    }

    // 生成消息体数据
    private static byte[] generateMessageBody(int vehicleControl) {
        ByteBuffer buffer = ByteBuffer.allocate(1);
        buffer.put((byte) vehicleControl);
        return buffer.array();
    }

    // 生成消息头
    private static byte[] generateMessageHeader(int messageId, int messageSeq, int messageBodyLength) {
        ByteBuffer buffer = ByteBuffer.allocate(12);
        buffer.putShort((short) messageId);
        buffer.putShort((short) messageBodyLength);
        buffer.putInt(messageSeq);
        buffer.put(new byte[8]); // 填充8个字节的终端手机号
        return buffer.array();
    }

    // 构建完整消息
    private static byte[] generateCompleteMessage(byte[] messageHeader, byte[] messageBody) {
        ByteBuffer buffer = ByteBuffer.allocate(messageHeader.length + messageBody.length);
        buffer.put(messageHeader);
        buffer.put(messageBody);
        return buffer.array();
    }

    // 将 byte[] 数据转换为十六进制字符串
    private static String bytesToHexString(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString().toUpperCase();
    }



        public static byte[] generateVehicleControlData(byte controlCommand, byte controlParameter) {
            // 构建车辆控制消息体数据
            byte[] data = new byte[4];
            data[0] = controlCommand;
            data[1] = controlParameter;
            data[2] = 0x00; // 保留字段，设置为0
            data[3] = 0x00; // 保留字段，设置为0

            return data;
        }




}